package com.example.qsmx.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserFromSession {
    public static String getPassword(String userAccount) throws SQLException {
        String userPassword = null;

        // 连接数据库
        Connection connection = null;
        connection = MyDBUtil.getConnection();

        // 创建 sql 语句获取数据库中所有的内容
        String valueSql = "SELECT userPassword FROM qsmxusers WHERE userAccount = ?";
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(valueSql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 传入参数
        preparedStatement.setString(1, userAccount);

        //获取数据集
        ResultSet ResultSet;
        try {
            ResultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 处理数据集返回密码
        while (ResultSet.next()){
            userPassword = ResultSet.getString("userPassword");
        }
        return userPassword;
    }
}
